﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using CaseReivew.Model;
using CaseReview.Model;
using CaseReview.Utility;

namespace Utility
{
    public class SendMail
    {
        public static void SendEmail(string to, string body,string subject) {
            SmtpClient client = new SmtpClient();
            MailMessage mailMessage = new MailMessage();
            client.Host = "GTECPDD.fareast.corp.microsoft.com";
            client.Port = 25;
            client.UseDefaultCredentials = true;
            if(to!=null) {
                string[] address = to.Split(';');
                foreach (string s in address) {
                    mailMessage.To.Add(s);
                }
            }
            MailAddress mailAddress = new MailAddress("procase@microsoft.com");
            mailMessage.From = mailAddress;
            mailMessage.Subject = subject;
            mailMessage.BodyEncoding = Encoding.GetEncoding(936);

            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;
            client.Send(mailMessage);
        }

        public static void SendEmail(string to,string body,string subject,string cc) {
            SmtpClient client = new SmtpClient();
            MailMessage mailMessage = new MailMessage();
            client.Host = "GTECPDD.fareast.corp.microsoft.com";
            client.Port = 25;
            client.UseDefaultCredentials = true;
            if(to != null) {
                string[] address = to.Split(';');
                foreach (string s in address) {
                    mailMessage.To.Add(s);
                }
            }
            if(cc != null && cc!="") {
                string[] address = cc.Split(';');
                foreach (string s in address) {
                    mailMessage.CC.Add(s);
                }
            }
            MailAddress mailAddress = new MailAddress("procase@microsoft.com");
            mailMessage.From = mailAddress;
            mailMessage.Subject = subject;
            mailMessage.BodyEncoding = Encoding.GetEncoding(936);

            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;
            client.Send(mailMessage);
        }
        public static void SendReviewerMail(IList<CaseInfo>caseInfos ) {
            string body;
            try {
                StreamReader reader = new StreamReader(Config.EmailTemp);
                body = reader.ReadToEnd();
            }
            catch (Exception) {
                return;
            }
            StringBuilder temp = new StringBuilder();
            foreach (CaseInfo caseInfo in caseInfos) {
                temp.Append(@"<tr><td>");
                temp.Append(caseInfo.CaseId).Append(@"</td><td>").Append(caseInfo.Title).Append(@"</td><td>")
                    .Append(caseInfo.Age).Append(@"</td><td>").Append(caseInfo.Labor).Append(@"</td><td>")
                    .Append(caseInfo.Idle).Append(@"</td><td>").Append(caseInfo.Owner).Append(@"</td><td>").Append(caseInfo.ReviewerName).Append(@"</td></tr>");
            }
            body = body.Replace("@@overall", temp.ToString());
            SendEmail(Config.ReviewerAddress, body, Config.Subject,Config.Cc);
        }

        public static void SendReminderMail(IList<CaseInfo> caseInfos) {
            string body;
            try {
                StreamReader reader = new StreamReader(Config.EmailTemp);
                body = reader.ReadToEnd();
            }
            catch (Exception) {
                return;
            }
            StringBuilder temp = new StringBuilder();
            foreach (CaseInfo caseInfo in caseInfos) {
                temp.Append(@"<tr><td>");
                temp.Append(caseInfo.CaseId).Append(@"</td><td>").Append(caseInfo.Title).Append(@"</td><td>")
                    .Append(caseInfo.Age).Append(@"</td><td>").Append(caseInfo.Labor).Append(@"</td><td>")
                    .Append(caseInfo.Idle).Append(@"</td><td>").Append(caseInfo.Owner).Append(@"</td><td>").Append(caseInfo.ReviewerName).Append(@"</td></tr>");
            }
            body = body.Replace("@@overall", temp.ToString());
            SendEmail(Config.ReviewerAddress, body, Config.ReminderSubject, Config.Cc);
        }
        public static void SendToCaseOwnerResultMail(CaseInfo caseInfo, ReviewResultInfo result,string ownerEmailAddress) {
            string body;
            try {
                StreamReader reader = new StreamReader(Config.ToOwnerEmail);
                body = reader.ReadToEnd();
            } catch (Exception) {
                return;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(@"<tr><td>").Append(caseInfo.CaseId).Append(@"</td><td>").Append(caseInfo.Title).Append(@"</td><td>")
                .Append(caseInfo.Age).Append(@"</td><td>").Append(caseInfo.Labor).Append(@"</td><td>").Append(caseInfo.Idle).Append(@"</td><td>")
                .Append(caseInfo.Severity).Append(@"</td><td>").Append(caseInfo.Phase).Append(@"</td><td>").Append(caseInfo.ServicelLevel).Append(@"</td><td>")
                .Append(caseInfo.Type).Append(@"</td><td>").Append(caseInfo.CountOfOwner).Append(@"</td><td>").Append(caseInfo.ReviewerName).Append(@"</td></tr>");
            body = body.Replace("@@Overall", sb.ToString());

            sb.Clear();

            body = body.Replace("@@Meet1", result.Cac1);
            body = body.Replace("@@comment1", result.Cac1C);
            body = body.Replace("@@Meet2", result.Cac2);
            body = body.Replace("@@comment2", result.Cac2C);
            body = body.Replace("@@Meet3", result.Cac3);
            body = body.Replace("@@comment3", result.Cac3C);
            body = body.Replace("@@MeetAdd", result.Cac4);
            body = body.Replace("@@commentAdd", result.Cac4C);
            body = body.Replace("@@Meet4", result.Bac1);
            body = body.Replace("@@comment4", result.Bac1C);
            body = body.Replace("@@Meet5", result.Bac2);
            body = body.Replace("@@comment5", result.Bac2C);
            body = body.Replace("@@Meet6", result.Bac3);
            body = body.Replace("@@comment6", result.Bac3C);

            body = body.Replace("@@Meet7", result.Nce1);
            body = body.Replace("@@comment7", result.Nce1C);
            body = body.Replace("@@Meet8", result.Nce2);
            body = body.Replace("@@comment8", result.Nce2C);
            body = body.Replace("@@Meet9", result.Nce3);
            body = body.Replace("@@comment9", result.Nce3C);

            body = body.Replace("@@replaceM1", result.Nce4);
            body = body.Replace("@@replaceC1", result.Nce4C);
            body = body.Replace("@@replaceM2", result.Nce5);
            body = body.Replace("@@replaceC2", result.Nce5C);
            body = body.Replace("@@replaceM3", result.Nce6);
            body = body.Replace("@@replaceC3", result.Nce6C);
            body = body.Replace("@@replaceM4", result.Nce7);
            body = body.Replace("@@replaceC4", result.Nce7C);
            body = body.Replace("@@replaceM5", result.Nce8);
            body = body.Replace("@@replaceC5", result.Nce8C);
            body = body.Replace("@@replaceM6", result.Nce9);
            body = body.Replace("@@replaceC6", result.Nce9C);
            body = body.Replace("@@replaceM7", result.Nce10);
            body = body.Replace("@@replaceC7", result.Nce10C);
            body = body.Replace("@@replaceC8", result.Osum);
            body = body.Replace("@@replaceC9", result.Osug);
            SendEmail(ownerEmailAddress, body, "Your case " + caseInfo.CaseId + " has been reviewed by " + caseInfo.ReviewerName, Config.ToOwnerCc);
        }
    }
}
